package cn.imvc.app.models;

import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import cn.imvc.app.entities.Activity;
import cn.imvc.app.entities.Red;
import cn.imvc.app.repositories.RedRepository;
import cn.imvc.core.ModelUtil;
import cn.imvc.data.domain.ViewRequest;

@Service
public class RedModelImpl implements RedModel{

	@Autowired
	RedRepository redRepository;
	
	public Page<Red> find(ViewRequest viewRequest) {
		return redRepository.findAll(ModelUtil.OrderByIdDesc(viewRequest));
	}

	public int save(Red red) {
		double amounts = red.getAmount()/100/red.getNum()/red.getPerNum();
		if(amounts<1){
			return 0;
		}
		if(red.getPerNum()<3){
			return -1;
		}
		redRepository.saveAndFlush(red);
		return 1;
	}

	public int update(int id,Red red) {
		Red loadRed = redRepository.getOne(id);
		double amounts = red.getAmount()/100/red.getNum()/red.getPerNum();
		if(amounts<1){
			return 0;
		}
		if(red.getPerNum()<3){
			return -1;
		}
		BeanUtils.copyProperties(red,loadRed,new String[]{"id"});
		redRepository.saveAndFlush(loadRed);
		return 1;
	}

	public Red get(int id) {
		return redRepository.getOne(id);
	}

	public void delete(int id) {
		redRepository.delete(id);
	}

}
